/**
 * 
 */
package com.witframework.baseapp;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.witframework.baseapp.common.enterprise.Organization;
import com.witframework.baseapp.common.enterprise.OrganizationType;
import com.witframework.baseapp.security.BaseUser;
import com.witframework.baseapp.security.service.UserService;
import com.witframework.cache.WitCacheFactory;
import com.witframework.core.AppInitializer;
import com.witframework.core.ApplicationProperties;
import com.witframework.core.WitFrameWork;
import com.witframework.core.logic.IBaseService;
import com.witframework.dynoengine.model.WitMetaColumn;
import com.witframework.dynoengine.model.WitMetaTable;

/**
 * @author wuxinyang Jul 8, 2009 11:07:27 AM
 */

public class UserInitializer implements AppInitializer {
	private static Log log = LogFactory.getLog(UserInitializer.class);
	public void execute() {
		long s = System.currentTimeMillis();
		initUser();		
		log.info("数据库初始化完成:" + (System.currentTimeMillis() - s) + "ms");
		initCache();
	}


	private void initCache() {
		String cacheClasses = ApplicationProperties.getProperty("hibernate.cache.class", "");
		String[] classes = StringUtils.split(cacheClasses, ",");
		for (String c : classes) {
			long s = System.currentTimeMillis();
			try {
				WitCacheFactory.getCache(Class.forName(c)).init();
			} catch (ClassNotFoundException e) {
				log.info("未找到需要缓存的实体类：" + c);
			}
			log.info(c + ":缓存加载完成:" + (System.currentTimeMillis() - s) + "ms");
		}
	}
	
	
	private void initUser() {			
		IBaseService sbs = WitFrameWork.getSimpleBaseService(OrganizationType.class);
		if (sbs.getAll().size() == 0) {
			OrganizationType orgType = new OrganizationType();
			orgType.setId("1");
			orgType.setName("Organization");
			sbs.save(orgType);
			orgType = new OrganizationType();
			orgType.setId("2");
			orgType.setName("Legal with accounting");
			sbs.save(orgType);
			orgType = new OrganizationType();
			orgType.setId("3");
			orgType.setName("Generic");
			sbs.save(orgType);
			orgType = new OrganizationType();
			orgType.setId("4");
			orgType.setName("Legal without accounting");
			sbs.save(orgType);
		}
		sbs = WitFrameWork.getSimpleBaseService(Organization.class);
		Organization org;
		if (sbs.getBy("orgname", "X公司") == null) {
			org = new Organization();
			org.setId("1");
			org.setOrgname("X公司");
			org.setAd_org_id("0");
			org.setOrganizationType((OrganizationType) WitFrameWork.getSimpleBaseService(OrganizationType.class).get("1"));
			sbs.save(org);
		}
		if (sbs.getBy("orgname", "A部门") == null) {
			org = new Organization();
			org.setId("1001");
			org.setOrgname("A部门");
			org.setAd_org_id("1");
			org.setOrganizationType((OrganizationType) WitFrameWork.getSimpleBaseService(OrganizationType.class).get("1"));
			sbs.save(org);
		}

		if (sbs.get("1001001") == null) {
			org = new Organization();
			org.setId("1001001");
			org.setOrgname("Z产品研发组");
			org.setAd_org_id("1001");
			org.setDescription("A product development group");
			org.setOrganizationType((OrganizationType) WitFrameWork.getSimpleBaseService(OrganizationType.class).get("1"));
			sbs.save(org);
		}

		if (sbs.getBy("orgname", "B部门") == null) {
			org = new Organization();
			org.setId("1002");
			org.setOrgname("B部门");
			org.setAd_org_id("1");
			org.setOrganizationType((OrganizationType) WitFrameWork.getSimpleBaseService(OrganizationType.class).get("1"));
			sbs.save(org);
		}
		sbs = WitFrameWork.getSimpleBaseService(BaseUser.class);
		if (sbs.getBy("username", "admin") == null) {
			BaseUser user = new BaseUser();
			user.setUsername("admin");		
			user.setName("Donald");
			user.setPassword("admin");
			user.setOrgId("1");
			sbs.save(user);
		}
		for (int i = 1; i <=100; i++) {			
			if (sbs.getBy("username", "user" + i) == null) {				
				BaseUser user = new BaseUser();
				user.setUsername("user" + i);
				user.setName("用户" + i);
				user.setPassword("user" + i);				
				user.setOrgId("1");
				user.setDescription("此用户账号仅为了测试系统功能");
				UserService us=(UserService)WitFrameWork.getBean("userService");
				us.save(user);//sbs.save(user);				
			}			
		}
	}	
	public static void main(String[] avg) {
		new UserInitializer().execute();
		new Thread1().start();		
	}
}
class Thread1 extends Thread {	
    public void run() {
    	IBaseService sbs = WitFrameWork.getSimpleBaseService(BaseUser.class);		
		for (int i = 1; i <=1; i++) {			
			if (sbs.getBy("username", "user" + i) == null) {				
				BaseUser user = new BaseUser();
				user.setUsername("user" + i);
				user.setName("用户" + i);
				user.setPassword("user" + i);				
				user.setOrgId("1");
				user.setDescription("此用户账号仅为了测试系统功能");
				UserService us=(UserService)WitFrameWork.getBean("userService");
				us.save(user);//sbs.save(user);				
			}			
		}
		System.out.println("AdUser="+sbs.getAllSize());			
    }
}

    

